'''
# Coding:utf-8
# Project: iiop
# Author: rtf
# Time: 2024-06-24 10:53:01
# FileName: software_hardware.py
# Software: PyCharm
'''

# 软硬件购置及开发实施模板 软硬件购置模板


# table1 各项费用投资分析表
def table1_data_process(data):
    try:
        result = {
            "software_cost": data[0][5],
            "hardware_cost": data[1][5],
            "cdf_workload": data[2][3], "cdf_labor_rate": data[2][4], "cdf_cost": data[2][5],
            "sfd_workload": data[3][3], "sfd_labor_rate": data[3][4], "sfd_cost": data[3][5],
            "sid_workload": data[4][3], "sid_labor_rate": data[4][4], "sid_cost": data[4][5],
            "si_workload": data[5][3], "si_labor_rate": data[5][4], "si_cost": data[5][5],
            "sii_workload": data[6][3], "sii_labor_rate": data[6][4], "sii_cost": data[6][5],
            "boe_workload": data[7][3], "boe_labor_rate": data[7][4], "boe_cost": data[7][5],
            "dprd_workload": data[8][3], "dprd_labor_rate": data[8][4], "dprd_cost": data[8][5],
            "dsiccq_workload": data[9][3], "dsiccq_labor_rate": data[9][4], "dsiccq_cost": data[9][5],
            "duddi_workload": data[10][3], "duddi_labor_rate": data[10][4], "duddi_cost": data[10][5],
            "other_cost": data[11][5],
            "total": data[12][5],
            "table1_evaluate": ""
        }
        return result
    except:
        return None


# table2 软硬件购置费用汇总表
def table2_data_process(data):
    # 初始化结果字典
    result = {
        "items": [],
        "subtotal": 0,
        "hpf_total": 0,
        "spf_total": 0,
        "table2_evaluate": ""
    }

    # 解析数据
    for row in data:
        if row[0] == '总计':
            result["spf_total"] = float(row[2])
            result["hpf_total"] = float(row[3])
            result["subtotal"] = float(row[4])
        else:
            item = {
                "unit": row[0],
                "dp": row[1],
                "spf": row[2],
                "hpf": row[3],
                "subtotal": float(row[4])
            }
            result["items"].append(item)
    return result


# table3 软硬件购置费用明细表
def table3_data_process(data):
    html = """
    <table style="border-collapse: collapse; width: 100%; height: 125.316pt;" border="1">
    <tbody>
        <tr style="height: 39.1406pt;">
            <td style="width: 11.0294%; text-align: center; height: 39.1406pt;"><span style="font-size: 10.5pt; font-family: 仿宋_GB2312;"><strong>出资单位</strong></span></td>
            <td style="width: 11.0294%; text-align: center; height: 39.1406pt;"><span style="font-family: 仿宋_GB2312;"><strong><span style="font-size: 10.5pt;">部署地</span></strong></span></td>
            <td style="width: 11.0294%; text-align: center; height: 39.1406pt;"><span style="font-family: 仿宋_GB2312;"><strong><span style="font-size: 10.5pt;">软硬件</span></strong></span></td>
            <td style="width: 11.0294%; text-align: center; height: 39.1406pt;"><span style="font-family: 仿宋_GB2312;"><strong><span style="font-size: 10.5pt;">设备类型</span></strong></span></td>
            <td style="width: 11.0294%; text-align: center; height: 39.1406pt;"><span style="font-family: 仿宋_GB2312;"><strong><span style="font-size: 10.5pt;">配置</span></strong></span></td>
            <td style="width: 11.0294%; text-align: center; height: 39.1406pt;"><span style="font-family: 仿宋_GB2312;"><strong><span style="font-size: 10.5pt;">单位</span></strong></span></td>
            <td style="width: 11.0294%; height: 39.1406pt; text-align: center;"><span style="font-family: 仿宋_GB2312;"><strong><span style="font-size: 10.5pt;">单价</span></strong></span></td>
            <td style="width: 11.0294%; height: 39.1406pt; text-align: center;"><span style="font-family: 仿宋_GB2312;">&nbsp;<strong><span style="font-size: 10.5pt;">数量</span></strong></span></td>
            <td style="width: 11.2132%; height: 39.1406pt; text-align: center;"><span style="font-family: 仿宋_GB2312;"><strong><span style="font-size: 10.5pt;">合计</span></strong></span></td>
        </tr>
    """

    current_unit = None
    current_location = None
    rowspans = {}

    # Calculate rowspans for each unit and location
    for row in data[:-1]:  # Exclude the total row for now
        unit = row[0]
        location = row[1]
        if (unit, location) not in rowspans:
            rowspans[(unit, location)] = 0
        rowspans[(unit, location)] += 1

    for row in data[:-1]:  # Exclude the total row for now
        unit = row[0]
        location = row[1]
        software = row[2]
        device_type = row[3]
        configuration = row[4]
        unit_type = row[5]
        unit_price = row[6]
        quantity = row[7]
        subtotal = row[8]

        if unit != current_unit or location != current_location:
            current_unit = unit
            current_location = location
            rowspan = rowspans[(unit, location)]
            html += f"""
            <tr style="height: 22.93pt;">
                <td style="height: 63.6328pt; text-align: center; width: 11.0294%;" rowspan="{rowspan}"><span style="font-family: 仿宋_GB2312;">{unit}</span></td>
                <td style="height: 63.6328pt; text-align: center; width: 11.0294%;" rowspan="{rowspan}"><span style="font-family: 仿宋_GB2312;">{location}</span></td>
            """
        else:
            html += "<tr style='height: 22.93pt;'>"

        html += f"""
            <td style="width: 11.0294%; text-align: center; height: 22.93pt;"><span style="font-family: 仿宋_GB2312;">{software}</span></td>
            <td style="width: 11.0294%; text-align: center; height: 22.93pt;"><span style="font-family: 仿宋_GB2312;">{device_type}</span></td>
            <td style="width: 11.0294%; text-align: center; height: 22.93pt;"><span style="font-family: 仿宋_GB2312;">{configuration}</span></td>
            <td style="width: 11.0294%; text-align: center; height: 22.93pt;"><span style="font-family: 仿宋_GB2312;">{unit_type}</span></td>
            <td style="width: 11.0294%; text-align: center; height: 22.93pt;"><span style="font-family: 仿宋_GB2312;">{unit_price}</span></td>
            <td style="width: 11.0294%; text-align: center; height: 22.93pt;"><span style="font-family: 仿宋_GB2312;">{quantity}</span></td>
            <td style="width: 11.2132%; text-align: center; height: 22.93pt;"><span style="font-family: 仿宋_GB2312;">{subtotal}</span></td>
        </tr>
        """

    # Add the total row
    total_row = data[-1]
    html += f"""
    <tr style="height: 22.2929pt;">
        <td style="height: 22.2929pt; text-align: center; width: 88.2353%;" colspan="8"><span style="font-family: 仿宋_GB2312;"><strong style="text-align: center;"><span style="font-size: 12pt;">总计</span></strong></span></td>
        <td style="width: 11.2132%; text-align: center; height: 22.2929pt;"><span style="font-family: 仿宋_GB2312;">{total_row[-1]}</span></td>
    </tr>
    """

    # Close the table
    html += """
    </tbody>
    </table>
    """
    return html